/**
 * Created by YiFeng on 2016/9/17.
 */



define(['ladder-core'], function (Ladder){

    Ladder.NS("html");
    Ladder.html = {
        REGX_HTML_ENCODE: /"|&|'|<|>|[\x00-\x20]|[\x7F-\xFF]|[\u0100-\u2700]/g,
        REGX_HTML_DECODE: /&\w+;|&#(\d+);/g,
        HTML_DECODE: { "&lt;": "<", "&gt;": ">", "&amp;": "&", "&nbsp;": " ", "&quot;": "\"", "&copy;": "" },
        /**
         * 转换HTML代码中的符号
         * @param {String} s 需要转换的代码
         * @returns {String}
         */
        encodeHTML: function(s) {
            s = (s != undefined) ? s : this.toString();
            return (typeof s != "string") ? s :
                s.replace(this.REGX_HTML_ENCODE,
                    function($0) {
                        var c = $0.charCodeAt(0), r = ["&#"];
                        c = (c == 0x20) ? 0xA0 : c;
                        r.push(c);
                        r.push(";");
                        return r.join("");
                    });
        },
        /**
         * 将转换后的HTML代码解码
         * @param s
         * @returns {*}
         */
        decodeHTML: function(s) {
            var HTML_DECODE = this.HTML_DECODE;
            s = (s != undefined) ? s : this.toString();
            return (typeof s != "string") ? s :
                s.replace(this.REGX_HTML_DECODE,
                    function($0, $1) {
                        var c = HTML_DECODE[$0];
                        if (c == undefined) {
                            if (!isNaN($1)) {
                                c = String.fromCharCode(($1 == 160) ? 32 : $1);
                            } else {
                                c = $0;
                            }
                        }
                        return c;
                    });
        }
    };


    //解析页面配置常量或变量
    Ladder.NS("Config");
    Ladder.Config={
        init:function(){
            $("[data-table]").each(function(){
                var $this=$(this);
                var tableName=$this.attr("data-table");
                if(/\{[\w\W]*\}/.test(tableName)){
                    tableName=tableName.replace("{","").replace("}","");
                    if(Tables[tableName]){
                        tableName=Tables[tableName];
                        $this.attr("data-table",tableName);
                    }
                }
            });
        },
        fromFile:function(url,key){
            var re_data=null;
            var dd=Ladder.webajax(url,"get","html","ddd",false);
            var jsondata=Ladder.toJSON(dd.responseText,true);
            if(key)return jsondata[key];
            else jsondata;
        }
    };

});